﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using YT.DB;
using YT.Commons;

namespace YT.XH.MF
{
    public partial class frmMmenusecurity : Form
    {
        public frmMmenusecurity()
        {
            InitializeComponent();
        }

        private void FillMenu()
        {
            string strSpace = "";
            string strSQL = string.Empty;
            if ( SysVar.CurrentMenuInfo.strMenuId.Substring (0,3)=="100")  
                strSQL ="select * from mMenu where MenuType='100' order by MenuID";
            else if ( SysVar.CurrentMenuInfo.strMenuId.Substring (0,3)=="200")
                strSQL = "select * from mMenu where MenuType='200' order by MenuID";
            else if (SysVar.CurrentMenuInfo.strMenuId.Substring(0, 3) == "300")
                strSQL = "select * from mMenu where MenuType='300' order by MenuID";
            
            
            DBAccess dbcon = new DBAccess(false, false);
            DataTable dtMenu = dbcon.GetDataTable(strSQL);
            foreach (DataRow drRow in dtMenu.Rows)
            {
                this.dgMenu.Rows.Add();
                if (CvtFunc.ToString(drRow["MenuType"]) == "100")
                    this.dgMenu.Rows[this.dgMenu.Rows.Count - 1].Cells[colType.Index ].Value = "梅尼埃病";
                if (CvtFunc.ToString(drRow["MenuType"]) == "200")
                    this.dgMenu.Rows[this.dgMenu.Rows.Count - 1].Cells[colType.Index ].Value = "突发性耳聋";
                if (CvtFunc.ToString(drRow["MenuType"]) == "300")
                    this.dgMenu.Rows[this.dgMenu.Rows.Count - 1].Cells[colType.Index ].Value = "良性阵发性位置性眩晕";

                if (CvtFunc.ToString(drRow["MenuID"]) == "100000000000")
                    strSpace = "";
                else if (CvtFunc.ToString(drRow["MenuID"]) == "200000000000")
                    strSpace = "";
                else if (CvtFunc.ToString(drRow["MenuID"]) == "300000000000")
                    strSpace = "";
                else 
                    strSpace = "    ";

                this.dgMenu.Rows[this.dgMenu.Rows.Count - 1].Cells[colMenuID.Index].Value =CvtFunc.ToString ( drRow["MenuID"]);
                this.dgMenu.Rows[this.dgMenu.Rows.Count - 1].Cells[colMenuName.Index].Value =strSpace + CvtFunc.ToString(drRow["MenuName"]);   
                this.dgMenu.Rows[this.dgMenu.Rows.Count - 1].Cells[colRecID.Index].Value = CvtFunc.ToString(drRow["RecID"]);
                this.dgMenu.Rows[this.dgMenu.Rows.Count - 1].Cells[colGroupNo1.Index].Value = CvtFunc.ToString(drRow["Permit01"]);
                this.dgMenu.Rows[this.dgMenu.Rows.Count - 1].Cells[colGroupNo2.Index].Value = CvtFunc.ToString(drRow["Permit02"]);
                this.dgMenu.Rows[this.dgMenu.Rows.Count - 1].Cells[colGroupNo3.Index].Value = CvtFunc.ToString(drRow["Permit03"]);
                this.dgMenu.Rows[this.dgMenu.Rows.Count - 1].Cells[colGroupNo4.Index].Value = CvtFunc.ToString(drRow["Permit04"]);
                this.dgMenu.Rows[this.dgMenu.Rows.Count - 1].Cells[colGroupNo5.Index].Value = CvtFunc.ToString(drRow["Permit05"]);               
            }
        }

        private bool UpdateData()
        {
            this.dgMenu.EndEdit();
            DBAccess dbcon = new DBAccess(false, false);
            foreach (DataGridViewRow dgvRow in this.dgMenu.Rows)
            {
                string strRecID = CvtFunc.ToString ( dgvRow.Cells[colRecID.Index].Value );
                string strPerMit01 = (CvtFunc.ToString ( dgvRow.Cells[colGroupNo1.Index ].Value)=="True" ? "1":"0")  ;
                string strPerMit02 = (CvtFunc.ToString ( dgvRow.Cells[colGroupNo2.Index].Value) == "True" ? "1" : "0");
                string strPerMit03 = (CvtFunc.ToString ( dgvRow.Cells[colGroupNo3.Index].Value) == "True" ? "1" : "0");
                string strPerMit04 = (CvtFunc.ToString ( dgvRow.Cells[colGroupNo4.Index].Value) == "True" ? "1" : "0");
                string strPerMit05 = (CvtFunc.ToString ( dgvRow.Cells[colGroupNo5.Index].Value) == "True" ? "1" : "0");
                string strSQL = "update mMenu set Permit01= '" + strPerMit01 + "', Permit02= '" + strPerMit02 + "', Permit03= '" + strPerMit03 + "', Permit04= '" + strPerMit04 + "', Permit05= '" + strPerMit05 + "' where RecID='" + strRecID + "' ";
                dbcon.ExecuteSql(strSQL);
            }
            dbcon.CloseCon();
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (UpdateData()) this.Close();
        }

        private void frmMmenusecurity_Load(object sender, EventArgs e)
        {
            this.Text = SysVar.CurrentMenuInfo.strMenuName;  
            FillMenu();
        }

        private void frmMmenusecurity_FormClosed(object sender, FormClosedEventArgs e)
        {
            SysVar.marrFormList.Remove(this.Name);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
